/**
 * Created by john on 2016/8/31.
 */
(function () {
    'use strict';

    angular
        .module('user.pay.rights.controller', [])
        .controller('UserPayRightsCtrl',UserPayRightsCtrl);

    UserPayRightsCtrl.$inject = ['$scope','$yikeUtils','$state','$rootScope','$ionicHistory','$ionicModal','$ionicTabsDelegate','$ionicPopup','localStorageService','$ionicPlatform','$sce'];
    /* @ngInject */
    function UserPayRightsCtrl($scope,$yikeUtils,$state,$rootScope,$ionicHistory,$ionicModal,$ionicTabsDelegate,$ionicPopup,localStorageService,$ionicPlatform,$sce) {
        $scope.$on('$ionicView.beforeLeave', function() {
            yikeTaishan.expire($scope.user.id,$scope.user.token)
                .then(function (data) {
                    if(data.status == 1){
                        localStorageService.set('user',data.result.user);
                    }
                })
        });
        $scope.select = {
            id:"",
            price:"",
            old_price:""
        };
        $scope.openQQ = openQQ;
        $scope.backpage = backpage;
        $scope.user=localStorageService.get('user');
        $scope.getsetWays = getsetWays;
        $scope.getList_id = getList_id;
        $scope.gonext = gonext;
        $scope.selectPlansNew = selectPlansNew;
        $scope.confirm = confirm;
        init();
        function init() {
            getsetWays();
            yikeTaishan.expire($scope.user.id,$scope.user.token)
                .then(function (data) {
                    if(data.status == 1){
                        getMessage();
                    }else{
                        var alertPopup = $ionicPopup.alert({
                            title: '提示',
                            template: data.result.result,
                            buttons:[{
                                text:'确定',
                                type: 'button-positive'
                            }]
                        });
                        alertPopup.then(function() {
                            localStorageService.remove('user');
                            $state.go('login')
                        });
                    }
                })
        }
        function openQQ(){
            cordova.InAppBrowser.open('mqqwpa://im/chat?chat_type=wpa&uin=3524944792&version=1&src_type=web&web_src=oicqzone.com','_system','location=no,toolbar=yes,toolbarposition=top,closebuttoncaption=关闭')
        }
        function getMessage() {
            yikeTaishan.personalCenter('platform','')
                .then(function (data) {
                    if(data.status == 1){
                        $scope.message=data.result;
                        $scope.$digest();
                    }
                })
        }

        function getsetWays(){
            yikeTaishan.setWays()
                .then(function (data) {
                    var pattern1=/&lt;/gim;
                    var pattern2=/&gt;/gim;
                    var pattern3=/&quot;/gim;
                    for(var i=0;i<data.result.result.length;i++){
                        if(data.result.result[i].content){
                            data.result.result[i].content=data.result.result[i].content.replace(pattern1,'<');
                            data.result.result[i].content=data.result.result[i].content.replace(pattern2,'>');
                            data.result.result[i].content=data.result.result[i].content.replace(pattern3,'"');
                            data.result.result[i].content =$sce.trustAsHtml(data.result.result[i].content);
                        }
                    }
                    $scope.allways = data.result.result;
                    console.log($scope.allways);
                    $scope.select.id = data.result.result[0].id;
                    $scope.select.price = data.result.result[0].price;
                    $scope.select.old_price = data.result.result[0].old_price;
                    $scope.$$phase || $scope.$digest();
                });
        }

        function getList_id(type,price){
            $scope.chose = {};
            $scope.chose.model_id = type;
            $scope.chose.price = price;
        }
        function gonext(){
            if ($scope.chose) {
                $state.go('pay-ways',{type:$scope.chose.model_id,price:$scope.chose.price})
                // cordova.InAppBrowser.open(WX_API_URL +'?i=1&c=entry&do=package&m=yike_ts_plan&op=recharge&token='+$scope.user.token+'&id='+$scope.model_id,'_system','location=no,toolbar=yes,toolbarposition=top,closebuttoncaption=关闭')
            }else{
                $yikeUtils.toast('请选择授权类型');
                return false;
            }
        }
        /*新的选择方式*/
        function selectPlansNew(i){
            $scope.select.id = i.id;
            $scope.select.price = i.price;
            $scope.select.old_price = i.old_price;
        }

        //返回上一页
        function backpage(){

            yikeTaishan.expire($scope.user.id,$scope.user.token)
                .then(function (data) {
                    if(data.status == 1){
                        localStorageService.set('user',data.result.user);
                    }
                })

            if($rootScope.buytype == 1){
                $state.go('tab.home');
            }else{
                $state.go('tab.account');
            }
        }
        //联系客服
        $ionicModal.fromTemplateUrl('templates/modal/service.html', {
            scope: $scope,
            animation: 'slide-in-up'
        }).then(function(modal) {
            $scope.service= modal;
        });
        $scope.openModal = function() {
            $scope.service.show();
        };
        $scope.closeModal = function() {
            $scope.service.hide();
        };
        $scope.$on('$destroy', function () {
            $scope.service.remove();
        });
        /*确认购买*/
        function confirm(i){
            /*window.open('http://114.215.222.89/app/index.php?i=1&c=entry&m=yike_ts_plan&do=pay&op=WeChat_Pay&price='+$scope.money+'&token='+$scope.user.token+'&id='+id.type+'&type=ALIWAP','_system');*/
            //这个为android的支付宝提交方式
            $scope.select.id = i.id;
            yikeTaishan.choseChargeWay("alipay",$scope.select.id,$scope.user.token)
                .then(function (data) {
                    var subStatus = data.result.set.alipay.alipay_pay;
                    if($ionicPlatform.is("IOS")){
                        if(subStatus){
                            if(data.result.list.type=='1'){
                                try{
                                    cordova.plugins.alipay.payment(data.result.list.form,function success(e){
                                        console.log(JSON.stringify(e));
                                        $yikeUtils.toast('支付成功');
                                        yikeTaishan.expire($scope.user.id,$scope.user.token)
                                            .then(function (data) {
                                                localStorageService.set("user",data.result.user);
                                            });
                                    },function error(e){
                                        console.log(JSON.stringify(e));
                                        $yikeUtils.toast('支付失败');
                                    });
                                }catch(ex){
                                    $yikeUtils.toast('支付未完成');
                                }
                            }
                        }else{
                            $ionicLoading.toast("支付功能已关闭");
                        }
                    }else{
                        if(data.result.list.type=='1'){
                            try{
                                cordova.plugins.alipay.payment(data.result.list.form,function success(e){
                                    console.log(JSON.stringify(e));
                                    $yikeUtils.toast('支付成功');
                                    yikeTaishan.expire($scope.user.id,$scope.user.token)
                                        .then(function (data) {
                                            localStorageService.set("user",data.result.user);
                                        });
                                },function error(e){
                                    console.log(JSON.stringify(e));
                                    $yikeUtils.toast('支付失败');
                                });
                            }catch(ex){
                                $yikeUtils.toast('支付未完成');
                            }
                        }
                    }
                });
            }
    }
})();
